package com.yy.controller.restaurantmanager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.yy.common.config.RCConfig;
import com.yy.common.core.AjaxResult;
import com.yy.common.core.model.RestaurantVM;
import com.yy.common.utils.DateUtils;
import com.yy.common.utils.JwtHelper;
import com.yy.common.utils.file.FileUploadUtils;
import com.yy.common.utils.file.MimeTypeUtils;
import com.yy.pojo.Restaurant;
import com.yy.service.RestaurantService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * ClassName: RestaurantController
 * Package: com.yy.controller.restaurantmanager
 * Description:
 *
 * @Author yy
 * @Create 2023-12-22 12:19
 * @Version 1.0
 */
@RestController
@RequestMapping("/restaurant-manager/restaurant-maintain")
public class RestaurantController {

    @Autowired
    private RestaurantService restaurantService;

    @Autowired
    private JwtHelper jwtHelper;


    @GetMapping("{id}")
    public AjaxResult queryCertainDetailDish(@PathVariable("id") Long userId) {
        Restaurant restaurant = restaurantService.getById(userId);
        RestaurantVM restaurantVM = new RestaurantVM();
        BeanUtil.copyProperties(restaurant, restaurantVM);
        restaurantVM.setOpeningTime(DateUtil.format(restaurant.getOpeningTime(), "HH:mm"));
        restaurantVM.setClosingTime(DateUtil.format(restaurant.getClosingTime(), "HH:mm"));
        return AjaxResult.success(restaurantVM);
    }


    @PutMapping("update")
    public AjaxResult update(@RequestPart(value = "restaurantPic", required = false) MultipartFile restaurantPic,
                             @RequestPart("restaurant") RestaurantVM restaurantVM,
                             @RequestHeader(value = "token", required = false) String token) throws Exception {

        if (ObjectUtils.isNotEmpty(restaurantPic) && !restaurantPic.isEmpty()) {
            // 更新头像
            String avatar = FileUploadUtils.upload(RCConfig.getDishPicPath(), restaurantPic, MimeTypeUtils.IMAGE_EXTENSION);
            restaurantVM.setAvatar(avatar);
        }

        Restaurant restaurant = new Restaurant();
        BeanUtil.copyProperties(restaurantVM, restaurant);
        restaurant.setOpeningTime(DateUtil.parse(restaurantVM.getOpeningTime(), "HH:mm"));
        restaurant.setClosingTime(DateUtil.parse(restaurantVM.getClosingTime(), "HH:mm"));
//        restaurant.setStatus("是".equals(restaurantVM.getStatus()) ? "1" : "0");
//        restaurant.setUpdateBy(String.valueOf(jwtHelper.getUserId(token)));
        restaurant.setUpdateTime(DateUtils.getNowDate());

        boolean update = restaurantService.updateById(restaurant);
        if (update) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

}
